package com.niit.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.niit.domain.News;
import com.niit.domain.Product;
import com.niit.service.INewsService;
import com.niit.service.IProductService;
import com.niit.service.impl.NewsServiceImpl;
import com.niit.service.impl.ProductServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "AllNewsServlet", value = "/AllNewsServlet")
public class AllNewsServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String currentPageStr = request.getParameter("currentPage");
        String pageSizeStr = request.getParameter("pageSize");

        int currentPage=0;
        if(currentPageStr!=null&&currentPageStr.length()>0){
            currentPage = Integer.parseInt(currentPageStr);
        }else {
            currentPage=1;
        }
        int pageSize =0;
        if(pageSizeStr!=null&&pageSizeStr.length()>0){
            pageSize = Integer.parseInt(pageSizeStr);
        }else {
            pageSize=15;
        }

        INewsService iNewsService = new NewsServiceImpl();
        domain.PageBean<News> News = iNewsService.queryProductsService(currentPage,pageSize);

        ObjectMapper mapper = new ObjectMapper();
        response.setContentType("application/json;charset=utf-8");
        mapper.writeValue(response.getOutputStream(),News);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
